package com.nuanshui.heatedloan.fund.integration.pay.execute.sysexecute;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.framework.security.AESUtil;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.PayType;
import com.nuanshui.heatedloan.fund.facade.PayPasswordService;
import com.nuanshui.heatedloan.fund.facade.UserFundActionService;
import com.nuanshui.heatedloan.fund.integration.form.PayForm;
import com.nuanshui.heatedloan.fund.integration.pay.annotation.PayHandlerComponent;
import com.nuanshui.heatedloan.fund.integration.pay.execute.PayConfirmHandler;
import com.nuanshui.heatedloan.fund.model.PayResult;
import com.nuanshui.heatedloan.fund.model.PayResult.PayResultStatusType;
import com.nuanshui.heatedloan.service.services.CommonTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;


@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@PayHandlerComponent(order=1,value="authWalletPayConfirmHandler",suportPayTypes={PayType.YE})
public class AuthWalletPayConfirmHandler implements PayConfirmHandler{


	private PayConfirmHandler payConfirmHandler;

	protected Logger logger = LoggerFactory.getLogger("pay_confirm");
	
	@Autowired
	private CommonTool  commonTool;

	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
	private PayPasswordService payPasswordService;

	@Reference(url=GlobalParam.FUND_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version=GlobalParam.FUND_DUBBO_SERVER_VERSION)
	private UserFundActionService userFundActionService;

	@Override
	public PayResult executePaymentConfirm(PayForm payForm) {
		
		if(!verifyPayPassword(payForm)) {//表示密码有误
			PayResult payResult = new PayResult();
			payResult.setStatus(PayResultStatusType.AUTH_FAIL);
			return payResult;
		}	
		if(!checkUserWalletBalance(payForm)){//表示与余额不足
			PayResult payResult = new PayResult();
			payResult.setStatus(PayResultStatusType.WALLET_PRICE_ERROR);
			return payResult;
		}	
		
		return payConfirmHandler.executePaymentConfirm(payForm);
	}
	
	
	
	/**
	 * 校验密码是否正确
	 * @param @return
	 * @author guohao
	 * @date 2017年1月10日 上午11:15:21
	 * @since $
	 */
	private boolean verifyPayPassword(PayForm payForm) {
		String pwd = payForm.getPassword();

		// 如果用余额支付
		if(commonTool.allowIOSExecute("110")){
			LogUtil.info(logger, "使用新版本的解密方式，解密前------"+ pwd);
			pwd = new String(AESUtil.decrypt(AESUtil.sixteenStrToByte(pwd)));
			LogUtil.info(logger, "使用新版本的解密方式，解密后------"+pwd);
		}else{
            LogUtil.info(logger, "使用老版本的解密方式，解密前------"+ pwd);
			pwd = commonTool.decryptPassword(payForm.getSafeToken(), payForm.getPassword());
			pwd = pwd.trim();
			pwd = pwd.substring(0, 6);
			pwd = SysStringUtils.reverse(pwd);
            LogUtil.info(logger, "使用老版本的解密方式，解密后------"+ pwd);
		}

		boolean verifyPayPwd = payPasswordService.verifyPayPassword(pwd, payForm.getUserId());
		return verifyPayPwd;
	}
	
	
	/**
	 * 校验钱包预约是否充足
	 * @param @return
	 * @author guohao
	 * @date 2017年1月10日 上午11:13:45
	 * @since $
	 */
	private boolean checkUserWalletBalance(PayForm payForm) {
		Long money = userFundActionService.loadTotalFund(payForm.getUserId()).asLong();
		if (money == null)
			money = 0l;
		if (money < payForm.getPayPrice().longValue()) {
			return  false;
		}
		return true;		
	}

	
	@Override
	public void setPayConfirmHandler(PayConfirmHandler payConfirmHandler) {

		this.payConfirmHandler = payConfirmHandler;
	}

}
